package com.lyz.moil.web.action;

import com.lyz.moil.core.security.entity.Resources;
import com.lyz.moil.core.security.entity.ResourcesGroup;
import com.lyz.moil.core.security.entity.User;
import com.lyz.moil.core.security.service.ResourcesService;
import com.lyz.moil.core.security.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xi
 * Date: 13-11-14
 * Time: 下午3:52
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping()
public class HomeAction {

    @Resource
    private ResourcesService resourcesService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/main.html")
    public String header(Model model, HttpServletRequest request) {
        User USER = (User) request.getSession().getAttribute("USER");

        Map<Object, Object> groupMap = new LinkedHashMap<Object, Object>();
        Map<Object, Object> menuMap = new LinkedHashMap<Object, Object>();

        List<Resources> setResuorces = resourcesService.findMenu(USER.getId());
        for (Resources r : setResuorces) {
            // 是菜单项才添加
            if (r.isMenu()) {
                ResourcesGroup group = r.getGroup();
                // 是菜单项才添加
                if (group.isMenu()) {
                    groupMap.put(group.getId().toString(), group);

                    @SuppressWarnings("unchecked")
                    List<Resources> list = (ArrayList<Resources>) menuMap.get(group.getId().toString());
                    if (list == null) {
                        list = new ArrayList<Resources>();
                    }
                    list.add(r);
                    menuMap.put(group.getId().toString(), list);
                }
            }
        }

        model.addAttribute("groupMap", groupMap);
        model.addAttribute("menuMap", menuMap);
        return "/main";
    }
}
